﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class OwnerControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        
        public void LoadOwnerData(int companyid, int propertyid)
        {
            int ownerid = RSPRMCommonFunctions.GetOwnerIDByPropertyID(companyid, propertyid);
            if (ownerid > 0)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                String sql = "SELECT OwnerID,FirstName,LastName,MiddleName,Title,Mobile,Phone1,Phone2,Fax,Email,Street, " +
                                      "City,State,Zip,Country,Note,CompanyID FROM RSPRM.dbo.rs_tblOwner " +
                                      "WHERE OwnerID=@OwnerID AND CompanyID=@CompanyID";

                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@CompanyID";
                param.Value = userIdentity.RSCompanyId;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@OwnerID";
                param.Value = ownerid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtEmail.Text = myDataReader["Email"].ToString();
                    txtFirstName.Text = myDataReader["FirstName"].ToString();
                    txtMiddleName.Text = myDataReader["MiddleName"].ToString();
                    txtMobile.Text = myDataReader["Mobile"].ToString();
                    txtLastName.Text = myDataReader["LastName"].ToString();
                    txtFax.Text = myDataReader["Fax"].ToString();
                    txtTitle.Text = myDataReader["Title"].ToString();
                    txtPhone1.Text = myDataReader["Phone1"].ToString();
                    txtPhone2.Text = myDataReader["Phone2"].ToString();
                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtCountry.Text = myDataReader["Country"].ToString();
                    txtNote.Text = myDataReader["Note"].ToString();
                }
                myConnection.Close();
            }
        }


    }
}